<?php
define('AROOT', dirname(__FILE__)); //AROOT ：为系统跟目录
$domain = explode('.',$_SERVER['HTTP_HOST']);
$domain = $domain[0];
if($domain=='www'){
	$domain = $_GET['domain'];
}
$cache_path = AROOT.'/temp/domain/'.$domain.'/data.bin';
if(!file_exists($cache_path)){
	$data = domain_data($domain,$cache_path);
}else{
	$data = unserialize(file_get_contents($cache_path));
}
if($data){
	domain_info($data);
}else{
	echo $domain.'.abans.cn 域名未申请'.'<a href="http://www.abans.cn/domain" target="_blank">点此申请</a>';
}

function domain_info($data){
	switch ($data['type']) {
		case 1:
			jump($data);
			break;
		case 2:
			space($data);
			break;
		case 3:
			page($data);
			break;
		case 4:
			site($data);
			break;
		default:
			page($data);
			break;
	}
}
//跳转
function jump($data){
	if(empty($data['url'])){page($data);return;}
	echo $data['jump_time'];
	echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
	echo '<meta http-equiv="refresh" content="'.$data['jump_time'].';url=http://'.$data['url'].'">';
	echo $data['content'];
}
//单页
function page($data){
	if(empty($data['content'])){
		$url = $data['domain'].'.abans.cn';
		echo '欢迎访问我的主页 <a href="http://'.$url.'" target="_blank">'.$url.'</a> <a href="http://www.abans.cn/center#domain" target="_blank">修改页面内容</a>';
	}
	echo $data['content'];
}
//主页
function space($data){
	$url = 'www.abans.cn';
	echo '改功能正在建设中 <a href="http://'.$url.'" target="_blank">'.$url.'</a>';
}
//网站
function site($data){
	$url = 'www.abans.cn';
	echo '改功能正在建设中 <a href="http://'.$url.'" target="_blank">'.$url.'</a>';
}


function domain_data($name,$path){
	$adb = new PDO("mysql:host=localhost;dbname=abans_web",'au','1002');
	$adb->query('set names utf8;');
	$sql = "select * from a_domain where if_del=0 and domain = '$name'";
	$domain = $adb->query($sql)->fetch();
	if($domain){
		Afun_file_mkdirm(dirname($path));
		file_put_contents($path,serialize($domain));
		return $domain;
	}else{
		return false;
	}
}

//递归创建文件夹
function Afun_file_mkdirm($path){
	if(!file_exists($path)){
		Afun_file_mkdirm(dirname($path));
		mkdir($path, 0777);
	}
	return true;
}

?>